package com.opensky.qatest.ui.page;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.ui.component.LoginPageSignInForm;
import com.opensky.qatest.util.common.AssertReport;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Login page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class LoginPage extends LoadableComponent<LoginPage> {

    // ************************ Locators ************************

    private static final String LOGIN_PAGE_URL = "https://opensky.com/login";

    private static final String SALES_PAGE_URL = "https://opensky.com/sales";

    private static final String LOGOUT_URL = "https://opensky.com/logout";

    private static final String LOGO = "//a[@class='logo']/img[@class='logo']";

    private static final String COPYRIGHT = "//div[@class='copyright']";

    private static final String JOIN_BTN_LINK = "// a[@class='button join-opensky']";

    private static final String FACEBOOK_LINK = "//a[text()='Log In With Facebook']";

    private static final String ALERT_MESSAGE_CONTAINER = "error-message";

    private static final String ALERT_MESSAGE_TEXT = "We didn't recognize the email or password you entered. Please try again.";
    
    // ************************ Complex page objects **************************

    private LoginPageSignInForm loginPageSignInForm;

    private final WebDriverWrapper driver;

    public LoginPage(WebDriverWrapper driver) {
	this.driver = driver;
	loginPageSignInForm = new LoginPageSignInForm(driver);
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(LOGIN_PAGE_URL);
    }

    public ResetPasswordPage clickResetPassword() {

	loginPageSignInForm.clickForgotPasswordLink();
	return new ResetPasswordPage(driver);
    }

    public void logOut() {
	driver.get(LOGOUT_URL);
    } 

    // ************************ Validations **************************

    @Override
    protected void isLoaded() throws Error {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(AssertReport.wrongURL(LOGIN_PAGE_URL, url), LogicUtils.isURLEqual(LOGIN_PAGE_URL, url));
    }
    
    public boolean isPageLoaded() {
	return driver.waitAndCheckURL(LOGIN_PAGE_URL);
    }

    public boolean isLogoAndCopyrightsPresent() {

	driver.waitAndCheckElementPresent("LOGO", By.xpath(LOGO));
	driver.waitAndCheckElementPresent("COPYRIGHT", By.xpath(COPYRIGHT));
	return true;
    }

    public boolean isJoinBtnAndFacebookLinkPresent() {

	driver.waitAndCheckElementPresent("JOIN_BTN_LINK", By.xpath(JOIN_BTN_LINK));
	driver.waitAndCheckElementPresent("FACEBOOK_LINK", By.xpath(FACEBOOK_LINK));
	return true;
    }

    public boolean isAlertMessagePresent() {
	driver.waitAndCheckElementPresent("ALERT_MESSAGE_CONTAINER", By.id(ALERT_MESSAGE_CONTAINER));
	String alert = driver.findElement(By.id(ALERT_MESSAGE_CONTAINER)).getText();
	return alert.contains(ALERT_MESSAGE_TEXT);
    }

    public boolean isLogedIn() {
	return LogicUtils.isURLEqual(driver.getCurrentUrl(), SALES_PAGE_URL);
    }

    public boolean isLogedOut() {
	return LogicUtils.isURLEqual(driver.getCurrentUrl(), LOGIN_PAGE_URL);
    }

    // ************************ GET/SET methods **************************

    public LoginPageSignInForm getSignInForm() {
	return loginPageSignInForm;
    }
}
